package cn.itcast.item.web;


import cn.itcast.hmall.config.MqConstants;
import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.item.Service.impl.itemImpl;
import cn.itcast.item.Service.itemService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

@RestController
@RequestMapping("/item")
public class itemController {

    @Autowired
    private itemService itemService;

    @Autowired
    RabbitTemplate rabbitTemplate;


    /**
     * 分页查询商品
     * @param searchItemDTO 商品后台搜索 请求参数封装类
     * @return
     */
    @PostMapping("/list")
    public PageDTO<Item> PaginatedQueries(@RequestBody SearchItemDTO searchItemDTO) {
        //返回结果
        IPage<Item> result =  itemService.PaginatedQueries(searchItemDTO);
                                  //总记录数            //当前页的数据
        return new PageDTO<>(result.getTotal(), result.getRecords());
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")  //接收请求路径中占位符的值
    public Item ByIdQuery(@PathVariable("id")Long id){

        return itemService.id(id);
    }

    /**
     * 新增商品
     * @param item
     * @return
     */
    @PostMapping
    public ResultDTO addGood(@RequestBody Item item){

        return itemService.add(item);
    }

    /**
     * 商品上架、下架
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public ResultDTO onDownGood(@PathVariable("id")Long id,@PathVariable("status")Integer status){
         //调用方法 发送消息
        rabbitTemplate.convertAndSend(
                MqConstants.ITEM_TOPIC_EXCHANGE,
                status == 1 ? MqConstants.ITEM_UP_ROUTING : MqConstants.ITEM_DOWN_ROUTING,id);

        return itemService.onDown(id,status);
    }


    /**
     * 修改商品
     * @param  item
     * @return ResultDTO
     */
    @PutMapping()
    public ResultDTO reviseGood(@RequestBody Item item){
        return itemService.revise(item);
    }

    /**
     * 删除商品
     * @param  id
     * @return ResultDTO
     */
    @DeleteMapping("/{id}")
    public ResultDTO DeleteGood(@PathVariable("id")Long id){
        return itemService.Delete(id);
    }


    @PutMapping("/stock/{itemId}/{num}")
    public void stock(@PathVariable Long itemId, @PathVariable Integer num) {
        itemService.stock(itemId, num);
    }

    // 补加库存
    @RequestMapping("/item/add/{id}/{num}")
    public ResultDTO add(@PathVariable("id") Long id, @PathVariable("num") Integer num) {
        UpdateWrapper<Item> itemUpdateWrapper = new UpdateWrapper<>();
        itemUpdateWrapper.eq("id", id).setSql("stock=stock+" + num);
        itemService.update(itemUpdateWrapper);
        return ResultDTO.ok();
    }
}
